import 'package:flutter/services.dart';
import 'package:flutter_getx/app/base/state_layout.dart';
import 'package:flutter_getx/core/ext_core.dart';
import 'package:flutter_getx/core/getx/control/base_controller.dart';
import 'package:flutter_getx/core/getx/control/locale_control.dart';
import 'package:flutter_getx/core/getx/control/theme_control.dart';
import 'package:flutter_getx/core/utils/log_utils.dart';
import 'package:flutter_getx/ext/ext_check.dart';
import 'package:flutter_getx/ui/main/model/index_data.dart';
import 'package:get/get.dart';
import 'package:rxdart/src/transformers/delay.dart';

import 'home_api.dart';
import 'home_state.dart';

class HomeLogic extends BaseController {
  final HomeState state = HomeState();

  late HomeApi _api;

  @override
  void onReady() {
    /// 显示状态栏和导航栏
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);

    ThemeControl.to.setTheme(ThemeControl.to.getThemeMode());

    _api = HomeApi();

    onRefresh();

    super.onReady();
    //ThemeControl.to.setTheme(ThemeMode.light);
  }

  @override
  void onClose() {
    state.scrollController.dispose();
    state.scrollControlWelfare.dispose();
    super.onClose();
  }

  Future onRefresh() async {
    return _api.loadData(true);
  }
  Future loadMore() async {
    //return _api.loadData(false);
  }

  void clickLogin(){
    Get.back();
    checkLogin(() {});
  }

  void switchTheme(){
    Get.back();
    ThemeControl.to.switchTheme();
  }

  void switchLanguage(){
    Get.back();
    LocaleControl.to.switchLocal();
  }


  void setIndexData(IndexData _data, {StateType emptyType = StateType.empty}){
    state.data = _data;
    if(_data.banner.nullSafe.isEmpty && _data.category.nullSafe.isEmpty && _data.welfareGoods.nullSafe.isEmpty && _data.recommendGoods.nullSafe.isEmpty){
      state.data = null;
      state.stateType.value = emptyType;
    }
    update();

    //lprint('home isDarkMode: ${Get.isDarkMode}');
  }

}
